/*
 * @lc app=leetcode.cn id=1460 lang=typescript
 *
 * [1460] 通过翻转子数组使两个数组相等
 */

// @lc code=start
// 只要元素都有，就可以翻转得到
function canBeEqual(target: number[], arr: number[]): boolean {
    if (target.length !== arr.length) return false;
    let cache = new Map<number, number>();

    target.forEach((i) => {
        cache.set(i, (cache.get(i) || 0) + 1);
    });
    for (let i = 0; i < arr.length; i++) {
        const element = arr[i];
        const count = cache.get(element);
        if (count) {
            cache.set(element, count - 1);
        } else {
            return false;
        }
    }
    return true;
}
// @lc code=end
